package com.zkw.gro.cloure.demo

/**
 * Created by Administrator on 2016/4/9 0009.
 */
class CurryMain {
    /**
     * 简单curry
     */
    def simple(){
        def adder = {x,y -> x+":"+y}
        def addOne = adder.curry(1)
        println(addOne(5))
    }
    /**
     * curry对日志的处理
     * 这是函数式编程的开始
     */
    def logCurry(){
        def configurator = {format,filter,line -> filter(line)?format(line):null}
        def appender = {config,append,line ->
            def out =config(line)
            if (out) append(out)
        }
        def dateFormatter = {line -> "${new Date()}: ${line}"}
        def dubugFilter = {line -> line.contains('debug')}
        def consoleAppender = {line -> println line}

        def myConf = configurator.curry(dateFormatter,dubugFilter)
        def myLog = appender.curry(myConf,consoleAppender)

        myLog('here is some debug message')
        myLog('this will not be printed')
    }

    public static void main(String[] args) {
        CurryMain curryMain = new CurryMain()
//        curryMain.simple()
        curryMain.logCurry()
    }

}
